package handlers

import (
	"cinema-pro/cache"
	"cinema-pro/models"
	"cinema-pro/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_err_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次,禁止登录",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败,用户不存在",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size

	var userList []models.User
	redis_key := "list_data_page" + strconv.Itoa(page)           //保证第一页的数据和第二页的数据不一样
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result() //从内存里读数据
	if cacheData == "" {
		//从mysql读取数据
		models.DB.Offset(offset).Limit(size).Find(&userList)

		//转化为json数据
		jsonData, _ := json.Marshal(userList)

		//保存到redis中
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		//如果内存里有数据,就直接拿出来用
		json.Unmarshal([]byte(cacheData), &userList)
	}
	models.DB.Offset(offset).Limit(size).Find(&userList)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": userList,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var user models.User
	redis_key := "user_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result() //从内存里读数据
	if cacheData == "" {
		//从mysql读取数据
		models.DB.Where("id=?", id).First(&user)

		//转化为json数据
		jsonData, _ := json.Marshal(user)

		//保存到redis中
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		//如果内存里有数据,就直接拿出来用
		json.Unmarshal([]byte(cacheData), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return

}

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	author := c.PostForm("author")
	if title == "" || content == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return

	}
	data := models.Article{
		Title:   title,
		Content: content,
		Author:  author,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
